<template>
	<el-container>
		<el-header>
			<el-button type="primary" @click="add_bt">添加</el-button>
		</el-header>
		<el-main>
			<el-table :data="list" style="width: 100%">
				<el-table-column label="封面">
					<template slot-scope="scope">
						<el-image style="width: 50px; height: 50px" :src="$get_img+scope.row.icon"></el-image>
					</template>
				</el-table-column>
				<el-table-column label="文章标题" prop="name">
				</el-table-column>
				<el-table-column label="导航" width="200">
					<template slot-scope="scope">
						{{scope.row.nav_show==1?'显示':'隐藏'}}
					</template>
				</el-table-column>
				<!-- <el-table-column label="文章详情" prop="content"> -->
				</el-table-column>
				<el-table-column align="right">
					<template slot-scope="scope">
						<el-button size="mini" @click="edit_bt(scope.row)">编辑</el-button>
						<el-button size="mini" type="danger" @click="del(scope.row.id)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>

			<el-dialog :title="edit_type==0?'添加':'编辑'" :visible.sync="visible" width="60%" :before-close="handleClose">
				<el-form ref="form" :model="form" label-width="80px">
					<el-form-item label="标题">
						<el-input v-model="form.name" class="input"></el-input>
					</el-form-item>
					
					<el-form-item label="封面">
						<Pic :url="form.icon" index="icon" @get_url="get_url" @delImg="delImg"></Pic>
					</el-form-item>
					
					<el-form-item label="导航显示">
						 <el-radio-group v-model="form.nav_show">
						    <el-radio :label="0">隐藏</el-radio>
							<el-radio :label="1">显示</el-radio>
						  </el-radio-group>
					</el-form-item>
					
					
					<el-form-item label="内容">
						<Edit :contents="form.content" :content_back="content_back"></Edit>
					</el-form-item>
					
					
					<el-form-item>
						<el-button type="primary" @click="submit">提交</el-button>
						<el-button type="primary" @click="handleClose">取消</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>

		</el-main>
		<el-footer>
			<el-pagination background layout="prev, pager, next" :page-size="search_form.num" :total="total"
				@current-change="current_change">
			</el-pagination>
		</el-footer>
	</el-container>
</template>
<script>
	import Pic from '../../components/UploadPicOne.vue'
	import Edit from '../../components/quill-editor.vue'
	export default {
		components:{
			Pic,
			Edit
		},
		data() {
			return {
				list: [],
				inputVisible: false,
				inputValue: "",
				tableName: "articles",
				form: {
					name: "",
					icon: "",
					nav_show: 1,
					content: "",
				},
				total: 0,
				url: ["/cms/table/insert", "/cms/table/update"],
				edit_tips: ["添加成功", "修改成功"],
				edit_type: 0, //0添加1编辑
				visible: false,
				search_form: {
					table: "",
					page: 1,
					num: 5,
					where: null,
					orderBy: "id",
					asdes: "asc"
				}
			}
		},
		mounted() {
			this.get_data()
		},
		methods: {
			get_data() {
				this.search_form.table = this.tableName
				const loading = this.$loading({
					lock: true,
					text: '加载中',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				});

				this.$model.post("cms/table/get", this.search_form).then(res => {
					if (res.code == 200) {
						loading.close();
						this.list = res.data.data
						this.total = res.data.total
					}
				})
			},
			content_back(e){
				this.form.content=e
			},
			handleInputConfirm() {
				this.inputVisible = false
				if (this.inputValue) {
					this.form.label.push(this.inputValue)
				}
				this.inputValue = ""
			},
			cancel_label(index) {
				this.form.label.splice(index, 1)
			},
			delImg(e){
				this.form[e]=""
			},
			get_url(e){
				this.form[e.index]=e.url
			},
			handleClose() {
				this.visible = false
			},
			current_change(e) {
				this.search_form.page = e
				this.get_data()
			},
			edit_bt(row) {
				this.edit_type = 1
				this.form = row
				this.visible = true
			},
			submit() {
				this.form["table"] = this.tableName
				this.$model.post(this.url[this.edit_type], this.form).then(res => {
					if (res.code == 200) {
						this.$message({
							message: this.edit_tips[this.edit_type],
							type: 'success'
						});
						this.clear()
						this.get_data()
					}
				})

			},
			clear() {
				this.edit_type = 0
				this.handleClose()
				this.form = {
						name: "",
						icon: "",
						nav_show: 1,
						content: "",
				}
			},
			add_bt() {
				this.visible = true
				this.edit_type = 0

			},
			del(id) {
				let url = "cms/table/del?table=articles&id=" + id
				this.$model.put(url).then(res => {
					if (res.code == 200) {
						this.$message({
							message: '已删除',
							type: 'success'
						});
						this.search_form.page = 1
						this.get_data()
					}
				})
			}
		}
	}
</script>
<style>
	.input{
		width: 300px;
	}
	
	.input-new-tag {
		width: 90px;
		margin-left: 10px;
		vertical-align: bottom;
	}
	
	
	.form {
		width: 400px;
	}
	
	.select {
		width: 100%;
	}
	
	.tags {
		margin-left: 10px;
	}
</style>
